package org.cnc.explain.lifecycle;

import org.cnc.explain.util.StepCount;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.ReaderHelper;

import javax.annotation.PostConstruct;

/**
 * org.cnc.spring.explain.lifecycle - LifeCycleBean
 *
 * @author tony-is-coding
 * @date 2022/7/4 17:25
 */
@Component
public class LifeCycleBean implements InitializingBean, DisposableBean, BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, ApplicationContextAware {
	public static final String BEAN_NAME = "lifeCycleBean";

	@PostConstruct
	public void postConstruct(){
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + " [%s] 执行Bean的PostConstruct注解的方法", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}


	public void init(){
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + " [%s] 执行Bean的初始化方法1", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}

	public void initMethod(){
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + " [%s] 执行Bean的初始化方法2", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}

	public LifeCycleBean() {
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + " [%s] 构造方法执行", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + "[%s] 属性设置完成", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + "[%s] 执行 Bean 销毁操作", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + "[%s] 执行BeanNameAware 回调", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}

	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + "[%s] 执行BeanClassLoaderAware 回调", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + "[%s] 执行 ApplicationContextAware 回调", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("\n\n");
		ReaderHelper.beanLifecycleTrace(StepCount.getStep() + "[%s] 执行 BeanFactoryAware callback", BEAN_NAME);
		ReaderHelper.formatTrace();
		System.out.println("\n\n");
	}
}
